<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>服务包管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../js/layui/css/layui.css"  media="all">
	<script src="../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
  
</head>
<body>
 
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add_admin">添加服务包</button>
  </div>
</script>
 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除服务包</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="edit">编辑服务包</a>
</script>
              
          
<script src="../js/layui/layui.js" charset="utf-8"></script>
 
<script>
layui.use(['table','layer','form'], function(){
  var form = layui.form,
	table = layui.table,
	layer = layui.layer;
  
  table.render({
    elem: '#test'
    ,url:'../static/server_bag_detail.json'
    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,defaultToolbar: ['filter', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
      title: '提示'
      ,layEvent: 'LAYTABLE_TIPS'
      ,icon: 'layui-icon-tips'
    }]
    ,title: '用户数据表'
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
      ,{field:'server_bag_name', title:'服务包', width:120}
			,{field:'server_bag_detail',title:'服务包内容',width:240}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:200}
    ]],
  });
  
  //头工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'add_admin':
        //layer.msg('添加管理员');
				layer.open({
					type: 1,
					title:"添加管理员",
					area: ['420px', '330px'],
					content: $('#popUpdateTest')
				});
				form.on("submit(demo11)", function (data) {
					alert(data.field.new_bag_name);
					 $.ajax({   
						url:'',
						method:'GET',
						data:{
							username : data.field.new_bag_name,
							password : data.field.new_bag_detail,
							identify : 'admin'
						},
						success:function(res){
							console.log(res);
							},              
							error:function (res) {
								console.log("失败"); 
							}           
					})  
				});
      break;
      
      //自定义头工具栏右侧图标 - 提示
      case 'LAYTABLE_TIPS':
        layer.alert('有问题请咨询售后工程师');
      break;
    };
  });
  
  //监听行工具事件
  table.on('tool(test)', function(obj){
    var data = obj.data;
    console.log(data);
    if(obj.event === 'del'){
      layer.confirm('真的删除该管理员吗？', function(index){
				console.log(data.phone);
				obj.del();
				layer.close(index);
         $.ajax({
            url: "/deleteCollector",
            type: "POST",
            data: {name: data.phone},
            success: function (msg) {
            var json = JSON.parse(msg);
            var returnCode = json.returnCode;
            if (returnCode == 200) {
								//删除这一行
							obj.del();
								//关闭弹框
              layer.close(index);
              layer.msg("删除成功", {icon: 6});
							} else {
                layer.msg("删除失败", {icon: 5});
              }
						}
					});
				return false;
      });
    } 
		else if(obj.event === 'edit'){
				layer.open({
					type: 1,
					title:"修改服务包",
					area: ['420px', '330px'],
					content: $('#popEditeTest')
				});
				
				form.val('edit_bag', {
				  "bag_name":data.server_bag_name
				  ,"bag_detail":data.server_bag_detail
				});
    }
  });
});
</script>

<!-- 弹出层 添加-->
<div class="layui-row" id="popUpdateTest" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" action="" style="margin-top:20px" >
            <div class="layui-form-item">
                <label class="layui-form-label">服务包名</label>
                <div class="layui-input-block">
                    <input type="text" name="new_bag_name"  required  lay-verify="required" autocomplete="off" placeholder="服务包名" class="layui-input">
                </div>
            </div>
						<div class="layui-form-item">
						    <label class="layui-form-label">服务内容</label>
						    <div class="layui-input-block">
						        <textarea rows="7" cols="32" name="new_bag_detail"></textarea>
						    </div>
						</div>
            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="demo11">确认</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- 弹出层 修改 -->
<div class="layui-row" id="popEditeTest" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" action="" style="margin-top:20px"  lay-filter="edit_bag">
            <div class="layui-form-item">
                <label class="layui-form-label">服务包名</label>
                <div class="layui-input-block">
                    <input type="text" name="bag_name"  required  lay-verify="required" autocomplete="off" placeholder="服务包名" class="layui-input">
                </div>
            </div>
						<div class="layui-form-item">
						    <label class="layui-form-label">服务内容</label>
						    <div class="layui-input-block">
						        <textarea rows="7" cols="32" name="bag_detail"></textarea>
						    </div>
						</div>
            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="demo11">确认</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
</html>
